<?php

//register_function('add_listing', 'check_referral_key');
register_function('url_interpret_end', 'referral_check_user');
register_function('admin_home', 'referral_admin_home');
//register_function('signup_handled', 'referral_code');
register_function('handle_signup_ajax', 'referral_signup_handled');
register_filter('handler_hidden_fields', 'referral_grab_code');

global $cn_registered_query_vars;
array_push($cn_registered_query_vars, 'referral_id');

function referral_signup_handled() {
	global $cndb;
	$id = $cndb->last_insert();
	?>
	<p>
	<input type="text" id="referral-id-link" value="<?=CN_URL?>?referral_id=<?=$id?>"/>
	</p>
	<p>Have three friends use the above link to sign up and you'll be accepted as a beta tester.</p>
	<?php
}

function referral_grab_code($fields) {
	global $cndb;
	$field = $cndb->get_row("SELECT * FROM `cn_listing_fields` WHERE `lf_column`='user_referred_by'");
	if(!$fields) $fields = array();
	$fields[] = array('field'=>$field, 'value'=>$_REQUEST['referral_id']);
	return $fields;
}

function referral_code() {
	global $cndb;
	
	$id = $cndb->last_insert();	
	// Generate code
	$code = str_rand(50, 'alphanum');
	$cndb->query("UPDATE `cn_users` SET `user_personal_referral_key`='$code' WHERE `user_id`='$id'");
	?>
	<p class="referral-key-container">
	Your referral key: <span class="referral-key"><?=$code?></span>
	</p>
	<?php
}


function check_referral_key($user) {
	global $cndb;
	$key = $_REQUEST['user_referral_key'];
	if($key) {
		$count = $cndb->get_count("SELECT `user_id` FROM `cn_users` WHERE `user_referral_key`='$key'");
		if($count >=3) {
			$user = $cndb->get_var("SELECT `user_id` FROM `cn_users` WHERE `user_personal_referral_key` = '$key'");
			$cndb->query("UPDATE `users` SET `user_level`='1' WHERE `user_id`='$user'");
		}
	}
}

function referral_check_user() {
	global $cndb;
	global $cn_current_user;
	$id = $cn_current_user->row->user_id;
	$count = $cndb->get_count("SELECT `user_id` FROM `cn_users` WHERE `user_referred_by`='$id'");
	$level = $cn_current_user->row->user_level;
	$status = cn_get_option('referral_site_status');
	switch($status) {
		case 'closed':
			if($level < 2) {
				include cn_template_dir() . 'site-closed.php';
				exit();
				
			}
			break;
		case 'referral_only':
			if($count < 3 && $level < 2) {
				include cn_template_dir() . 'referral-only.php';
				exit();
				
			}
			break;
	}
}

function referral_admin_home() {
	if($_REQUEST['referral_site_status']) {
		cn_set_option('referral_site_status', $_REQUEST['site_status']);
		?><p class="alert-success">Site status updated.</p><?php
	}
	?>
	<div class="box">
		<h3>Site Status</h3>
		<div class="box-content">
			<form action="<?= $_SERVER['PHP_SELF']?>" method="post">
				<p>
					<?php $status = cn_get_option('referral_site_status');?>
					<select name="site_status">
						<option value="closed">Closed</option>
						<option <?=($status == 'referral_only') ? 'selected="selected"' : ''?> value="referral_only">Referral Only</option>
						<option <?=($status == 'open') ? 'selected="selected"' : ''?> value="open">Open</option>
					</select>
				<p><input type="submit" class="small-button" name="referral_site_status" value="Update Status"/></p>
			</form> 
		</div>
	</div>
	<?php
}

?>